package parser;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

public class YToltParser implements IParser {

	@Override
	public void parse(String filePath) {
		
		/*格式如：
		 * YTOLT-J13(config)#show fdbentry mac 1/4
			4-EPN(slot1)/port4 mac-learning list
			total Learned mac counter=184

			   Mac addr         type      by OnuIdx
			-----------------------------------------
			000f.e906.6842     dynamic      1
			14e6.e4a6.ef1d     dynamic      1
			cc00.0000.0022     dynamic      2
			0012.d670.4063     dynamic      2
		 */
		File file=new File(filePath);
		String olt = file.getName();
		olt = olt.substring(0,olt.indexOf(".log"));
		File file1=new File(filePath+".out");
		try {
			if(!file.exists()||file.isDirectory())
			    throw new FileNotFoundException();
			
			
			FileWriter fw=new FileWriter(file1);
			BufferedReader br=new BufferedReader(new FileReader(file));
			String temp=br.readLine();
			String[] onu;
			String pon="";
			int i=0;
			while(temp!=null){	
				if (temp.indexOf("show fdbentry") > 0) {
					pon = temp.split("\\s+")[3];
				}
				if (temp.indexOf("dynamic") >0) {
					onu = temp.split("\\s+"); //以n个空格切分
					fw.write("id:"+i+",olt:"+olt+",eoc mac:"+onu[0]+",getOnuPort:" + pon + "/" + onu[2]+"\n");
					i++;					
				}	
			    temp=br.readLine();
			}
			br.close();
			fw.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("deal "+filePath+" error!" );
		} 
    }	

	public Vector<String> preParse(String midData) {
		try{
			BufferedReader br = new BufferedReader(new InputStreamReader(
													new ByteArrayInputStream(midData.getBytes())
												   ));  
			String temp=br.readLine();
			String[] onu;
			String[] ids;
			Vector<String> retv = new Vector();
			
			/*格式形如:
			 * pon12/3 [onu counter = 14]
				 1  000f.e904.2414  YTONU2-4        up       0147:22:24:16  GT811
				 2  000f.e904.bc82  YTONU2-4        up       0003:22:51:24  GT811_A
				 3  000f.e904.247c  YTONU2-4        up       0001:01:03:19  GT811
			   pon12/3 [onu counter = 12]				 	 
			 * */
			while(temp!=null){	
				if (temp.startsWith("pon")) {
					onu = temp.split("\\s+");
					String pon = onu[0].substring(3,onu[0].length());					
					retv.add(pon);
				}	
			    temp=br.readLine();			    
			}
			br.close();
			return retv;
		}catch (Exception ex){
			ex.printStackTrace();
		}		
		return null;
	}
	
	public static void main(String[] args) {
		String temp = "pon12/3 [onu counter = 14]";
		String[] onu = temp.split("\\s+");
		String pon = onu[0].substring(3,onu[0].length());
		System.out.println(pon);

	}

}
